﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace BudgetMealsModels.Entities
{
    public class User
    {
        [HiddenInput]
        public int UserID { get; set; }
        [Required]
        [MaxLength(20)]
        public String FirstName { get; set; }
        [Required]
        [MaxLength(20)]
        public String LastName { get; set; }
        [Required]
        [MaxLength(15)]
        public String UserName { get; set; }
        [Required]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$")]
        public String Email { get; set; }
        [DataType(DataType.MultilineText)]
        [AllowHtmlAttribute]
        [MaxLength(1000)]
        public String Biography { get; set; }
        [InverseProperty("User")]
        public List<Recipe> Recipes { get; set; }
        [InverseProperty("User")]
        public List<Meal> Meals { get; set; }
        [HiddenInput]
        public double ChefRating { 
            get {
                if (Recipes != null && Recipes.Count > 0){
                    IEnumerable<Recipe> ratedRecipes = Recipes.Where(x => x.Rating != 0);
                    if(ratedRecipes != null && ratedRecipes.Count() > 0)
                        return Math.Round(ratedRecipes.Average(r => r.Rating));
                }
                return 0;
            } }
        [HiddenInput(DisplayValue = false)]
        public byte[] ImageData { get; set; }
        [HiddenInput(DisplayValue = false)]
        public string ImageMimeType { get; set; }
    }
}
